package com.tobestronger.shouxiespringframework.context.annotation;

import cn.hutool.core.util.ClassUtil;
import com.tobestronger.shouxiespringframework.beans.factory.config.BeanDefinition;
import com.tobestronger.shouxiespringframework.stereotype.Component;

import java.util.LinkedHashSet;
import java.util.Set;

/**
 * 扫描指定路径下的类生成bean定义信息
 */
public class ClassPathScanningCandidateComponentProvider {

    /**
     * 扫描basePackage下所有被Component注解的类到Set<BeanDefinition>
     *
     * @param basePackage
     * @return
     */
    public Set<BeanDefinition> findCandidateComponents(String basePackage) {
        Set<BeanDefinition> candidates = new LinkedHashSet<>();
        Set<Class<?>> classes = ClassUtil.scanPackageByAnnotation(basePackage, Component.class);
        for (Class<?> clazz : classes) {
            candidates.add(new BeanDefinition(clazz));
        }
        return candidates;
    }

}
